﻿package com.godpaper.controllers.mxml.ds
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.*;
	
	import mx.core.UIComponent;
	
	public class Cell extends UIComponent
	{
		public static var SIZE:int = 20;
		public static var COUNT:int = 0;
		
		public var cellIndex:int;
		public var radius:int;
		
		public var x0:Number, y0:Number;
		
		public var inserted:Boolean;
		
		public function Cell()
		{
			cellIndex = COUNT++;
			radius = SIZE;
			draw();
			inserted = false;
			
			addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
			addEventListener(MouseEvent.MOUSE_UP,   mouseHandler);
		}
		
		public function setPos(x:Number, y:Number):void
		{
			this.x = x0 = x;
			this.y = y0 = y;
		}
		
		public function deconstruct():void
		{
			removeEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseHandler);
			removeEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
			removeEventListener(MouseEvent.MOUSE_UP, mouseHandler);
			removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			
			parent.removeChild(this);
			
			COUNT--;
		}
		
		public function draw():void
		{
			graphics.clear();
			graphics.lineStyle(2, 0x999999, 1);
			graphics.beginFill(0XFFFFFF, 1);
			graphics.drawRect(-SIZE/2, -SIZE/2, SIZE, SIZE);
			graphics.endFill();
			
			var tf:TextField = new TextField();
//			tf.defaultTextFormat = new TextFormat(new DemoFont().fontName, 11, 0, false, false, null, null, null, "center");
//			tf.embedFonts = true;
			tf.antiAliasType = "advanced";
			tf.text = cellIndex.toString();
			tf.autoSize = "left";
			tf.selectable = false;
			tf.x -= tf.width / 2;
			tf.y -= tf.height / 2;
			tf.mouseEnabled = false;
			tf.name = "cellId";
			addChild(tf);
		}
		
		public function getYOffset():Number
		{
			return Math.abs( (y - y0) / (ArrayExample.SH - y0 - radius) );
		}
		
		public function insert():void
		{
			inserted = true;
			this.startDrag(false);
			
			x = stage.mouseX;
			y = stage.mouseY;
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);
		}
		
		public function mouseHandler(e:MouseEvent):void
		{
			if (ArrayExample(parent).tweening) return;
			
			if (e.type == MouseEvent.MOUSE_DOWN)
			{
				if (COUNT <= 2) return;
				
				this.startDrag(false, new Rectangle(Math.max(x, radius), radius, 0, ArrayExample.SH - 2 * radius));
				stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);
				stage.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
				removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
			}
			else
			if (e.type == MouseEvent.MOUSE_UP)
			{
				this.stopDrag();
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);
				stage.removeEventListener(MouseEvent.MOUSE_UP, mouseHandler);
				
				if (getYOffset() > .9 && COUNT >= 2)
					ArrayExample(this.parent).onCellRelease(this);
				else
					addEventListener(Event.ENTER_FRAME, enterFrameHandler);
				
				if (inserted)
				{
					//insert node
				}
				else
				{
					//remove node
					//if (getYOffset() > .9 && COUNT >= 3)
						//LinkedList(this.parent).onNodeRelease(this);
					//else
					//drag node around
						//addEventListener(Event.ENTER_FRAME, enterFrameHandler);
				}
			}
			else
			if (e.type == MouseEvent.MOUSE_MOVE)
			{
				if (!inserted)
				{
					if (getYOffset() > .9)
						this.alpha = .25;
					else
						this.alpha = 1;
				}
			}
		}
		
		public function enterFrameHandler(e:Event):void
		{
			var dy:Number = y - y0;
			y -= dy * .3;
			
			if (Math.abs(dy) <= 1)
			{
				removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
				y = y0;
			}
		}
		
		override public function toString():String
		{
			return "[Cell, id=" + cellIndex + "]";
		}
	}
}